namespace GameKit.Core
{
    public class Timer
    {
        // PRIVATE
        private float _elapsedTime;
        private readonly float _duration;

        // PUBLIC
        public delegate void TimerCompleted(Timer timer);

        public event TimerCompleted OnCompleted;

        public Timer()
        {
        }

        public Timer(float duration)
        {
            _duration = duration;
            _elapsedTime = 0;
        }

        public static Timer Start(float duration)
        {
            var timer = new Timer(duration);
            return timer;
        }

        public static Timer Start(float duration, TimerCompleted completed)
        {
            var timer = new Timer(duration);
            timer.OnCompleted += completed;
            return timer;
        }

        public void OnUpdate(float deltaTime)
        {
            _elapsedTime += deltaTime;
            if (_elapsedTime >= _duration)
            {
                _elapsedTime = 0;
                OnCompleted?.Invoke(this);
            }
        }
    }
}